<!--
 * @Author: ChunLai
 * @Date: 2022-09-21 17:50:48
 * @LastEditTime: 2025-08-29 14:37:35
 * @Description: 老接口聚合首页
 * @FilePath: \02.bldinsure\src\views\form\components\AssuredBenefit.vue
-->
<template>
  <div class="insuPage">
    <div class="topSec">
      <div class="form-head-name">受益人信息</div>
      <div class="flex_ac"></div>
    </div>
    <div>
      <van-field label="受益人类型" input-align="right" label-width="100px">
        <div slot="input">
          <MsRadios
            :curVal="getBbrObj.bnfCategory"
            :arrList="bnfTypeList"
            @change="changeBenefitType"
          >
          </MsRadios>
        </div>
      </van-field>
      <!-- 受益人列表 -->
      <div v-if="getBbrObj.bnfCategory == '1'">
        <div class="tips_txt ftc4">
          最多可选择{{ benefyOrderMax }}个受益顺位,每个顺位最多可添加{{
            benefyNumMax
          }}个受益人
        </div>
        <template v-if="getbnfList.length > 0">
          <div
            class="userCell"
            v-for="(item, index) in getbnfList"
            :key="index"
          >
            <AsuBenefitItem
              :ref="`bef${index}`"
              :key="`${bindex}bef${index}`"
              :bindex="bindex"
              :pindex="index"
            ></AsuBenefitItem>
          </div>
        </template>
        <!-- 计算下受益份额 等于100不显示  -->
        <div class="addCell" @click="addBenefits()" v-if="syrCanAddMore">
          <MsImgIcon name="add" />
          <span class="ft15 ml4 lh_20">添加受益人</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import AsuBenefitItem from "./AsuBenefitItem";
import { userRelateRisk } from "@/translate/validate/index";
export default {
  name: "AssuredBenefit",
  props: {
    // 被保人索引
    bindex: {
      type: Number,
      default: () => 0,
    },
  },
  components: { AsuBenefitItem },
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      bbrMultList: "insert/bbrMultList",
      benefyOrderMax: "page/benefyOrderMax",
      benefyNumMax: "page/benefyNumMax",
      bnfTypeList: "dictionary/bnfTypeList",
      mainInsu: "risks/mainInsu",
    }),
    getBbrObj() {
      let res = {};
      if (this.bbrMultList.length > 0) {
        res = this.bbrMultList[this.bindex];
      }
      return res;
    },
    getbnfList() {
      let res = [];
      if (this.getBbrObj.bnfs && this.getBbrObj.bnfs.length > 0) {
        res = this.getBbrObj.bnfs;
      }
      return res;
    },
    syrMaxNum() {
      return this.benefyOrderMax * this.benefyNumMax;
    },
    syrCanAddMore() {
      const numLen = this.getbnfList.length;
      if (numLen >= this.syrMaxNum) {
        return false;
      }
      return true;
    },
  },
  created() {},
  methods: {
    saveForm() {
      // console.log("1---", this.getBbrObj);
      if (this.getBbrObj.bnfCategory == "1") {
        let resObj = { status: true, breakTips: [] };
        const bnfLen = this.getbnfList.length;
        // console.log(bnfLen);
        if (bnfLen > 0) {
          for (let index = 0; index < bnfLen; index++) {
            let refname = `bef${index}`;
            let flag = this.$refs[refname][0].saveForm();
            if (!flag.status) {
              resObj = flag;
              break;
            }
            if (flag.breakTips && flag.breakTips.length > 0) {
              resObj.breakTips = [...resObj.breakTips, ...flag.breakTips];
            }
          }
          return resObj;
        } else {
          this.$store.commit("common/SET_FIELD_MEAN_CONT", {
            label: `受益人规则校验`,
            desc: `受益人类型选择: "指定" ,请添加受益人`,
          });
          return { status: false };
        }
      } else {
        return { status: true };
      }
    },
    /**
     * 受益人变更
     */
    changeBenefitType(type, item) {
      const zhuRisk = JSON.parse(JSON.stringify(this.mainInsu[0]));
      // 选择指定受益人
      if (item.value == "1") {
        let res2 = userRelateRisk(zhuRisk, item.value);
        if (res2.status == false) {
          this.$store.commit("common/SET_FIELD_MEAN_CONT", {
            label: `规则校验`,
            desc: `${res2.msg}`,
          });
          return;
        }
        // 附属被保险人
        if (zhuRisk.attachedBbrForbid && this.bindex > 0) {
          return this.$toast(`${zhuRisk.attachedBbrForbid}`);
        }
      }

      let obj = {
        index: this.bindex,
        data: { bnfCategory: item.value },
      };
      if (item.value != "1") {
        obj.data.bnfs = [];
      }
      this.$store.commit("insert/UPDATE_MULT_BBR", obj);
      this.$forceUpdate();
    },
    /**
     * 增加受益人
     */
    addBenefits() {
      // 受益人总数校验
      if (!this.syrCanAddMore) {
        return this.$toast("根据保司要求,受益人个数已达上限");
      }
      this.$store.dispatch("insert/addNewSyr", this.bindex);
    },
  },
};
</script>
<style lang="less" scoped>
@import "../style.less";

.tips_txt {
  padding: 5px;
  text-align: center;
  font-size: 12px;
  background: #ffffff;
}
</style>
